package com.cskaoyan.service;

import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.mapper.MarketAdMapper;
import com.cskaoyan.mapper.MarketCouponMapper;
import com.cskaoyan.mapper.MarketCouponUserMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AdvertisingServiceImpl implements AdvertisingService {
    @Autowired
    MarketAdMapper marketAdMapper;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCouponUserMapper marketCouponUserMapper;

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Override
    public AdminAdListVo getOrderAdList(Integer page, Integer limit, String name, String content, String sort,
                                        String order) {
        AdminAdListVo adminAdListVo = new AdminAdListVo();
        adminAdListVo.setLimit(limit);
        adminAdListVo.setPage(page);

        //查询total
        MarketAdExample marketAdExample = new MarketAdExample();
        sort = sort + " " + order;
        marketAdExample.setOrderByClause(sort);

        //设置模糊查询，如果有
        if (name != null && !"".equals(name)) {
            name = '%' + name + '%';
            MarketAdExample.Criteria criteriaName = marketAdExample.createCriteria();
            criteriaName.andNameLike(name);
        }
        if (content != null && !"".equals(content)) {
            content = '%' + content + '%';
            MarketAdExample.Criteria criteriaContent = marketAdExample.createCriteria();
            criteriaContent.andContentLike(content);
        }
        List<MarketAd> marketAds = marketAdMapper.selectByExample(marketAdExample);
        int total = marketAds.size();
        adminAdListVo.setTotal(total);
        int pages = total / limit + 1;
        adminAdListVo.setPages(pages);

        //获取当页数据
        PageHelper.startPage(page, limit);
        List<MarketAd> marketAdsCurrentPage = marketAdMapper.selectByExample(marketAdExample);
        for (MarketAd marketAd : marketAdsCurrentPage) {
            adminAdListVo.setListElements(marketAd.getDeleted(), marketAd.getAddTime(), marketAd.getName(),
                    marketAd.getLink(),
                    marketAd.getUpdateTime(), marketAd.getId(), Integer.valueOf(marketAd.getPosition()),
                    marketAd.getUrl(), marketAd.getContent(), marketAd.getEnabled());
        }
        return adminAdListVo;
    }

    @Override
    public AdminAdUpdateVo updateAdvertising(AdminAdUpdateBo updateBo) {
        //根据id更新数据库
        MarketAd marketAd = new MarketAd();
        marketAd.setDeleted(updateBo.isDeleted());
        marketAd.setAddTime(updateBo.getAddTime());
        marketAd.setName(updateBo.getName());
        marketAd.setLink(updateBo.getLink());
        marketAd.setUpdateTime(updateBo.getUpdateTime());
        marketAd.setId(updateBo.getId());
        marketAd.setPosition((byte) updateBo.getPosition());
        marketAd.setUrl(updateBo.getUrl());
        marketAd.setContent(updateBo.getContent());
        marketAd.setEnabled(updateBo.isEnabled());
        marketAdMapper.updateByPrimaryKey(marketAd);

        //更新VO
        AdminAdUpdateVo updateVo = new AdminAdUpdateVo();
        updateVo.setAddTime(updateBo.getAddTime());
        updateVo.setName(updateBo.getName());
        updateVo.setLink(updateBo.getLink());
        updateVo.setUpdateTime(updateBo.getUpdateTime());
        updateVo.setId(updateBo.getId());
        updateVo.setPosition(updateBo.getPosition());
        updateVo.setUrl(updateBo.getUrl());
        updateVo.setContent(updateBo.getContent());
        updateVo.setEnabled(updateBo.isEnabled());
        return updateVo;
    }

    @Override
    public void deleteAdvertising(AdminAdDeleteBo deleteBo) {
        marketAdMapper.deleteByPrimaryKey(deleteBo.getId());
    }

    @Override
    public AdminAdCreateVo createAdvertising(AdminAdCreateBo createBo) {
        //插入数据库
        Date date = new Date();
        MarketAd marketAd = new MarketAd();
        marketAd.setName(createBo.getName());
        marketAd.setLink(createBo.getLink());
        marketAd.setPosition((byte) createBo.getPosition());
        marketAd.setContent(createBo.getContent());
        marketAd.setUrl(createBo.getUrl());
        marketAd.setEnabled(createBo.isEnabled());
        marketAd.setAddTime(date);
        marketAd.setUpdateTime(date);
        marketAdMapper.insertSelective(marketAd);

        //设置VO
        //格式化时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        AdminAdCreateVo createVo = new AdminAdCreateVo();
        createVo.setId(marketAd.getId());
        createVo.setAddTime(simpleDateFormat.format(date));
        createVo.setLink(createBo.getLink());
        createVo.setUpdateTime(simpleDateFormat.format(date));
        createVo.setPosition(createBo.getPosition());
        createVo.setUrl(createBo.getUrl());
        createVo.setContent(createBo.getContent());
        createVo.setEnabled(createBo.isEnabled());
        return createVo;
    }

    @Override
    public AdminCouponListVo getCouponList(Integer page, Integer limit, String name, Short type, Short status,
                                           String sort, String order) {
        AdminCouponListVo listVo = new AdminCouponListVo();
        MarketCouponExample couponExample = new MarketCouponExample();

        //查询total
        sort = sort + " " + order;
        couponExample.setOrderByClause(sort);
        //设置模糊查询，如果有
        if (name != null && !"".equals(name)) {
            name = '%' + name + '%';
            MarketCouponExample.Criteria criteriaName = couponExample.createCriteria();
            criteriaName.andNameLike(name);
            if(type != null){
            criteriaName.andTypeEqualTo(type);
            }
            if(status != null){
            criteriaName.andStatusEqualTo(status);
            }
        }else if(type != null){
        }
        if (type != null) {
            MarketCouponExample.Criteria criteriaType = couponExample.createCriteria();
            criteriaType.andTypeEqualTo(type);
            if(status != null){
                criteriaType.andStatusEqualTo(status);
            }
        }else if(status != null){
            couponExample.createCriteria().andStatusEqualTo(status);
        }

        PageHelper.startPage(page,limit);
        List<MarketCoupon> marketCoupons = marketCouponMapper.selectByExample(couponExample);
        PageInfo pageInfo = new PageInfo(marketCoupons);
        listVo.setTotal((int) pageInfo.getTotal());
        listVo.setPages(pageInfo.getPages());
        listVo.setPage(page);
        listVo.setLimit(limit);
        for (MarketCoupon marketCoupon : marketCoupons) {
            listVo.setCouponList(marketCoupon);
        }

        return listVo;
    }

    @Override
    public AdminCouponCreateVo createCoupon(AdminCouponCreateBo createBo) {
        //插入操作
        MarketCoupon marketCoupon = new MarketCoupon();
        Date date = new Date();
        //对goodsValueList进行处理

        if(createBo.getGoodsValue().size()==0){
            List<?> goodsValue = new ArrayList();
            marketCoupon.setGoodsValue(goodsValue);
        }else if(createBo.getGoodsType()==1){
            //商品限制类型，如果0则全商品，如果是1则是类目限制，如果是2则是商品限制
            System.out.println("待实现");
        } else {
            System.out.println("缺失数据待实现");
        }
        marketCoupon.setAddTime(date);
        marketCoupon.setUpdateTime(date);
        //生成一个随机兑换码
        String[] uuid = UUID.randomUUID().toString().split("-");
        String code = uuid[2]+uuid[3];
        marketCoupon.setCode(code.toUpperCase());

        BeanUtils.copyProperties(createBo,marketCoupon);
        marketCouponMapper.insertSelective(marketCoupon);

        //生成VO对象
        AdminCouponCreateVo createVo = new AdminCouponCreateVo();

        BeanUtils.copyProperties(marketCoupon,createVo);
        return createVo;
    }

    @Override
    public AdminCouponUpdateVo updateCoupon(AdminCouponUpdateBo updateBo) {
        MarketCoupon marketCoupon = new MarketCoupon();
        AdminCouponUpdateVo updateVo = new AdminCouponUpdateVo();

        BeanUtils.copyProperties(updateBo,marketCoupon);

        String[] uuid = UUID.randomUUID().toString().split("-");
        String code = uuid[2]+uuid[3];

        marketCoupon.setCode(code.toUpperCase());
        marketCouponMapper.updateByPrimaryKey(marketCoupon);

        BeanUtils.copyProperties(marketCoupon,updateVo);

        updateVo.setCode(code);
        return updateVo;
    }

    @Override
    public void deleteCoupon(AdminCouponDeleteBo deleteBo) {
        marketCouponMapper.deleteByPrimaryKey(deleteBo.getId());
    }

    @Override
    public AdminCouponReadVo getCouponById(Integer id) {
        AdminCouponReadVo readVo = new AdminCouponReadVo();
        MarketCoupon marketCoupon = marketCouponMapper.selectByPrimaryKey(id);

        BeanUtils.copyProperties(marketCoupon, readVo);
        List<?> goodsValue = new ArrayList<>();
        readVo.setGoodsValue(goodsValue);
        return readVo;
    }

    @Override
    public AdminCouponListUserVo getListUser(Integer page, Integer limit, Integer couponId, Integer userId,
                                             Short status, String sort, String order) {
        AdminCouponListUserVo listUserVo = new AdminCouponListUserVo();
        listUserVo.setLimit(limit);
        listUserVo.setPage(page);

        MarketCouponUserExample userExample = new MarketCouponUserExample();
        sort = sort + " " + order;
        userExample.setOrderByClause(sort);

        //设置查找条件
        if (userId != null) {
            MarketCouponUserExample.Criteria criteria = userExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
        }
        if (status != null) {
            MarketCouponUserExample.Criteria criteria = userExample.createCriteria();
            criteria.andStatusEqualTo(status);
        }

        //查询total
        List<MarketCouponUser> couponUsers = marketCouponUserMapper.selectByExample(userExample);
        int total = couponUsers.size();
        listUserVo.setTotal(total);
        int pages = total / limit + 1;
        listUserVo.setPages(pages);

        //获取当页数据
        PageHelper.startPage(page, limit);
        List<MarketCouponUser> couponUsersCurrentPage = marketCouponUserMapper.selectByExample(userExample);
        listUserVo.setList(couponUsersCurrentPage);
        return listUserVo;
    }

    @Override
    public AdminTopicListVo getTopicList(Integer page, Integer limit, String title, String subtitle, String sort,
                                         String order) {
        AdminTopicListVo topicListVo = new AdminTopicListVo();
        topicListVo.setLimit(limit);
        topicListVo.setPage(page);

        //查询total
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        sort = sort + " " + order;
        marketTopicExample.setOrderByClause(sort);
        //设置模糊查找条件
        if (title != null && !"".equals(title)) {
            title = '%' + title + '%';
            MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
            criteria.andTitleLike(title);
            if (subtitle != null && !"".equals(subtitle)) {
                subtitle = '%' + subtitle + '%';
                criteria.andSubtitleLike(subtitle);
            }
        }
        if (subtitle != null && !"".equals(subtitle)) {
            subtitle = '%' + subtitle + '%';
            MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
            criteria.andSubtitleLike(subtitle);
        }

        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(marketTopicExample);
        int total = marketTopics.size();
        topicListVo.setTotal(total);
        int pages = total / limit + 1;
        topicListVo.setPages(pages);

        //查询本页数据
        PageHelper.startPage(page, limit);
        List<MarketTopic> marketTopicCurrentPage = marketTopicMapper.selectByExampleWithBLOBs(marketTopicExample);
        for (MarketTopic marketTopic : marketTopicCurrentPage) {
            topicListVo.setListEntity(marketTopic);
        }
        return topicListVo;
    }

    @Override
    public AdminTopicCreateVo createTopic(AdminTopicCreateBo createBo) {
        AdminTopicCreateVo createVo = new AdminTopicCreateVo();
        MarketTopic marketTopic = new MarketTopic();

        BeanUtils.copyProperties(createBo, marketTopic);
        Date date = new Date();
        marketTopic.setAddTime(date);
        marketTopic.setUpdateTime(date);
        marketTopic.setGoods(createBo.getGoods());
        marketTopicMapper.insertSelective(marketTopic);

        BeanUtils.copyProperties(marketTopic, createVo);
        createVo.setGoods(createBo.getGoods());
        return createVo;
    }

    @Override
    public AdminTopicReadVo readTopic(Integer id) {
        AdminTopicReadVo readVo = new AdminTopicReadVo();
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);

        readVo.setTopicEntity(marketTopic);
        return readVo;
    }

    @Override
    public AdminTopicUpdateVo updateTopic(AdminTopicUpdateBo updateBo) {
        AdminTopicUpdateVo updateVo = new AdminTopicUpdateVo();
        MarketTopic marketTopic = new MarketTopic();

        BeanUtils.copyProperties(updateBo, marketTopic);
        marketTopic.setGoods(updateBo.getGoods());
        marketTopicMapper.updateByPrimaryKeyWithBLOBs(marketTopic);

        BeanUtils.copyProperties(updateBo, updateVo);
        return updateVo;
    }

    @Override
    public void deleteTopic(AdminTopicDeleteBo deleteBo) {
        marketTopicMapper.deleteByPrimaryKey(deleteBo.getId());
    }
}
